package com.itheima.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author 张义
 * @describe:
 * @date 2021/06/19
 */

@Component("myAspect")
@Aspect // 告知 Spring 容器 MyAspect 类是一个切面类
public class MyAspect {

    @Before("pointcut()")
    public void before(){
        System.out.println("前置增强......");
    }

    @AfterReturning("pointcut()")
    public void afterReturning(){
        System.out.println("后置增强......");
    }

    // Proceeding JoinPoint : 正在执行的连接点 === 切点
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前增强......");
        Object proceed = joinPoint.proceed();// 切点参数
        System.out.println("环绕后增强......");
        return proceed;
    }

    @AfterThrowing("pointcut()")
    public void afterThrowing(){
        System.out.println("异常抛出增强......");
    }

    @After("pointcut()")
    public void after(){
        System.out.println("最终增强......");
    }


    @Pointcut("execution(* com.itheima.anno.*.*(..))")
    public void pointcut(){
        // 切点表达式抽取 , 该方法不需要实现 , 因为注解需要依附于一个实体
        // 调用可以使用 pointcut  和  MyAspect.pointcut  ,
    }
}
